﻿using GrainManage.Data.Model;
using GrainManage.Data.Service.Util;
using System;
using System.Collections.Generic;
using System.Data;

namespace GrainManage.Data.Service
{
    public class CommonService : BaseService, GrainManage.Data.Service.Interfaces.ICommonService
    {
        public CommonService(GrainManage.Data.Access.IAccess proc) : base(proc) { }

        public List<string> GetSpecifiedColumn(string table, string column, string columnValue, IPageable page, string creator)
        {
            var fieldList = new List<string>();
            DataSet type = Proc.ExecuteQuery(ProcConst.Common_GetByTableColumnExtent, table, column, columnValue, page.PageIndex, page.PageSize, creator);
            foreach (DataRow item in type.Tables[0].Rows)
            {
                if (item[0] != DBNull.Value)
                {
                    fieldList.Add(item[0] as string);
                }
            }

            return fieldList;
        }

        public T ExecuteSQL<T>(string cmdText, bool forUpdate)
        {
            T result = default(T);
            Type type = typeof(T);
            if (forUpdate)
            {
                if (type == typeof(int))
                {
                    object affected = Proc.Execute(ProcConst.Common_ExecuteSQL, cmdText);
                    result = (T)affected;
                }
            }
            else
            {
                if (type.IsValueType || type == typeof(string))
                {
                    object ds = Proc.ExecuteScalar(ProcConst.Common_ExecuteSQL, cmdText);
                    result = (T)ds;
                }
                else if (type == typeof(DataSet) || type == typeof(object))
                {
                    object ds = Proc.ExecuteQuery(ProcConst.Common_ExecuteSQL, cmdText);
                    result = (T)ds;
                }
            }
            return result;
        }
    }
}
